<?php
	include "./include/om_config.inc";

  $smarty = new SmartyWWW();
  $fromDate = isset($_POST['fromDateYear'])? $_POST['fromDateYear']."-".strtoupper($_POST['fromDateMonth'])."-".$_POST['fromDateDay']:mktime(0, 0, 0, date("m")  , date("d")-1, date("Y"));
  $smarty->assign("fromDate",$fromDate);
  
  /////For Display in table ////
  
         
	$expiryDateBc=array();
	$highPrice=0;
	$lowPrice=0;
	$closePrice=0;
	$exchangeValue = array();
	
	//Variables for NL Calculations : start
	$range = 0;
	$fixedValue = array(11.80,23.60,38.20,61.80,100.00,127.20,161.80,261.80,423.60);
	$countFixedValue = count($fixedValue);
	$fixedCalculation = array();
	$upside = array();
	$upsideBlack = array();
	$down = array();
	$downBlack = array();
	$down1 = array();
	$down2 = array();
	
	
	$selectQuery = "SELECT exchange,contractCode,expiryDate FROM bhavcopyf_o 
	                ORDER BY bhavcopyDate,expiryDate";
	$resultQuery = mysql_query($selectQuery);
	$count=0;
	while($rowQuery = mysql_fetch_array($resultQuery))
	{
		$contract[$count] = $rowQuery['contractCode'];
		$expiry[$count] = $rowQuery['expiryDate'];
		$exchange[$count] = $rowQuery['exchange'];
		$count++;
	}
	$currentItemId   = isset($_POST['itemId'])? $_POST['itemId'] :0;
	$expDate         = isset($_POST['expiryDate'])? $_POST['expiryDate'] : $expiry[0]; 
	$currentExchange = isset($_POST['exchange'])? $_POST['exchange'] : $expiry[0]; 
	
	$displayCount = 0;  
	$displayQuery = "SELECT * FROM bhavcopyf_o WHERE bhavcopyDate = '".$fromDate."'";
	$displayQuery .= " AND contractCode='".$currentItemId."'";
	
	if($currentExchange !='BSE CASH')
	{ 
	 $displayQuery .= " AND expiryDate='".$expDate."'"; 
	}
	$displayQuery .="  AND exchange='".$currentExchange."' ORDER BY bhavcopyDate,contractCode,expiryDate";
	$displayResult = mysql_query($displayQuery);
	$fixed = 0;
	if($displayRow = mysql_fetch_array($displayResult))
	{   
	  $highPrice  			 = $displayRow['highPrice'];
	  $lowPrice 				 = $displayRow['lowPrice'];
	  $closePrice  		   = $displayRow['closePrice'];
	  $range             = $highPrice - $lowPrice;
	  for($fixed=0;$fixed < $countFixedValue;$fixed++)
	  {
	  	
	    $fixedCalculation[$fixed][$displayCount] = $range * ($fixedValue[$fixed]/100);
	    $upside[$fixed][$displayCount] = $closePrice + $fixedCalculation[$fixed][$displayCount];
	    $down[$fixed][$displayCount] = $closePrice - $fixedCalculation[$fixed][$displayCount];
	    if($fixed >= 1)
	    {
	      $down1[$fixed][$displayCount] = $down[$fixed - 1][$displayCount]- $down[$fixed][$displayCount];
	      $down2[$fixed][$displayCount] = $down1[$fixed][$displayCount] / 2;
	      $upsideBlack[$fixed][$displayCount] = $upside[$fixed - 1][$displayCount] + $down2[$fixed][$displayCount];
	      $downBlack[$fixed][$displayCount] = $down[$fixed][$displayCount] + $down2[$fixed][$displayCount];
	    }
	  }
	  $displayCount++;
	}
  
  $down1[0]="0";
  $down2[0]="0";
  $upsideBlack[0]="0";
  $downBlack[0]="0";
  ////Request parameters, if passed : transfer to proper variable :End
  
  //Item records :Start
  $itemIdValues = array();
  $itemIdOptions = array();
  $itemCount = 0;
  $itemRecords = array();
  $selectedExchange = isset($_POST['exchange'])? $_POST['exchange'] :'F_O';
  $smarty->assign("selectedExchange",$selectedExchange);
  
  $itemQuery = "SELECT DISTINCT(contractCode) FROM bhavcopyf_o
                WHERE exchange='".$selectedExchange."' 
                ORDER BY contractCode";
  $itemResult = mysql_query($itemQuery);
  while($itemRow = mysql_fetch_array($itemResult))
  {
  	if($itemCount == 0 && strlen($currentItemId) == 1)
      $currentItemId = $itemRow['contractCode'];
      
    $itemIdValues[$itemCount]  = $itemRow['contractCode'];
    $itemIdOptions[$itemCount] = $itemRow['contractCode'];
    $itemCount++;
  }
  
  //Expiry records :Start
  if(isset($_POST['expiryDate']))
  {
    if($_POST['itemIdChanged']==1)
      $expiryDateSelected = 0;
    else
      $expiryDateSelected = $_POST['expiryDate'];
  }
  else
    $expiryDateSelected = 0;
    
  $expiryDateValues = array();
  $expiryDateOptions = array();
  
  $i=0;
  
  $expiryQuery = "SELECT DISTINCT(expiryDate),expiryDateBc FROM bhavcopyf_o 
                  WHERE contractCode='".$currentItemId."'
                  ORDER BY expiryDate";
  $expiryResult = mysql_query($expiryQuery);
  while($expiryRow = mysql_fetch_array($expiryResult))
  {
      $expiryDateValues[$i]  = $expiryRow['expiryDate'];
      $expiryDateOptions[$i] = $expiryRow['expiryDateBc'];
      $i++;
  }
  
  $selectExchange="SELECT DISTINCT (exchange) FROM bhavcopyf_o";
  $resultExchange = mysql_query($selectExchange);    
  $j=0;
  while($rowExchange = mysql_fetch_array($resultExchange))
  {
  	$exchangeValue[$j] = $rowExchange['exchange'];
  	$j++;
  }
   
  //////End for display in table //////////
  
  


  $smarty->assign("highPrice",$highPrice);
  $smarty->assign("lowPrice",$lowPrice);
  $smarty->assign("closePrice",$closePrice);
  
  $smarty->assign("range",$range);
  $smarty->assign("fixedValue",$fixedValue);
  $smarty->assign("fixedCalculation",$fixedCalculation);
  $smarty->assign("upside",$upside);
  $smarty->assign("upsideBlack",$upsideBlack);
  $smarty->assign("down",$down);
  $smarty->assign("downBlack",$downBlack);
  $smarty->assign("down1",$down1);
  $smarty->assign("down2",$down2);
  
  $smarty->assign("currentItemId",$currentItemId);
  $smarty->assign("itemIdValues",$itemIdValues);
  $smarty->assign("itemIdOptions",$itemIdOptions);
  $smarty->assign("expiryDateSelected",$expiryDateSelected);
  $smarty->assign("expiryDateValues",$expiryDateValues);
  $smarty->assign("expiryDateOptions",$expiryDateOptions);
  $smarty->assign("exchangeValue",$exchangeValue);
  
  $smarty->display('dayNLF_O.tpl');
  ?>